using UnityEngine;
using System.Collections;

public class MoveCylinderSpace : MonoBehaviour
{
    public GameObject hand;
    public moveShipSpace moveShip;
    public float speed;
    public MoveCylinderSpace cylinder;
    // Use this for initialization
    void Start()
    {
		// Find the hand object from the scene and attach the component to moveShipSpace script.
        hand = GameObject.Find("Hand");
        moveShip = hand.GetComponent<moveShipSpace>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 movement = new Vector3(0, 0, 1);
        transform.Translate(movement * Time.deltaTime * speed);
        
		// Checks that the ball is in screen still.
        if (transform.position.y < -7 || transform.position.x < -9 || transform.position.x > 9)
        {
            if (gameObject.renderer.enabled == true && moveShip.isDeath == false)
            {
                moveShip.health -= 1;
            }
            else 
            {
                gameObject.renderer.enabled = true;
            }
			// Create the next object and instantiate it to screen
			// After that, increase the new object speed and destroy the object which has 
			// left the screen.
			
            MoveCylinderSpace nextCylinder;
            nextCylinder = (MoveCylinderSpace)Instantiate(cylinder, new Vector3(Random.Range(-7.4f, 7.3f), 6, -0.5f), Quaternion.Euler(90, 0, 0));
            nextCylinder.enabled = true;
            nextCylinder.speed = speed + Random.Range(0.2f, 0.6f);
            Destroy(gameObject);
        }
    }
}
